package view.components
{
	import core.Control;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import utils.ColorTool;
	import utils.DisplayTool;

	public class Button extends Sprite
	{
		private var _id:int;
		protected var _onClickFunction:Function;
		protected var _control:Control = Control.getInstance();
		protected var _isUpInfo:Boolean;
		protected var _info:String;
		protected var isVaryColor:Boolean = true;//是否变换颜色
		public var isLight:Boolean = true;
		
		
		public function Button(x:int,y:int,id:int,onClickFunction:Function = null)
		{
			_id = id;
			_onClickFunction = onClickFunction;
			this.x = x;
			this.y = y;
			initAll();
		}
		
		private function initAll():void
		{
			initListeners();
		}
		
		public function set enabled(v:Boolean):void
		{
			this.buttonMode = v;
			super.mouseEnabled = v;
		}
		
		private function initListeners():void
		{
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			if ( !isLight)
			{
				addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			}
			addEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		private function removeListeners():void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			if ( !isLight)
			{
				removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
				removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			}
			removeEventListener(MouseEvent.CLICK,onMouseClick);
		}
		
		public function destroy():void
		{
			removeListeners();
			DisplayTool.removeAllChild(this);
			_control = null;
			_onClickFunction = null;
		}
		
		public function set info(v:String):void
		{
			_info = v;
		}
		
		public function set isUpInfo(v:Boolean):void
		{
			_isUpInfo = v;
		}

		protected function onMouseUp(event:MouseEvent):void
		{
			if ( isLight)
			{
				ColorTool.lightButton(this);
			}
		}
		
		protected function onMouseDown(event:MouseEvent):void
		{
			if ( isLight)
			{
				ColorTool.lightButtonYellow(this);
			}
		}
		
		protected function onMouseOver(event:MouseEvent):void
		{
			if (_info)
			{
				_control.hintControl.showHint(this,"<font color='#663300' size='12'>" + _info + "</font>",_isUpInfo);
			}
			
			if ( isLight)
			{
				ColorTool.lightButton(this);
			}
			else
			{
				ColorTool.addBrightness(this);
			}
		}
		
		override public function set mouseEnabled(enabled:Boolean) : void
		{
			super.mouseEnabled = enabled;
			if ( !isVaryColor)
			{
				return;
			}
			if (enabled)
			{
				ColorTool.saturation(this);
				addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			}
			else
			{
				ColorTool.desaturation(this);
				removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
				if (_info!= null && _control.hintControl != null)
				{
					_control.hintControl.hideHint();
				}
			}
		}
		
		protected function onMouseOut(event:MouseEvent):void
		{
			if (_info)
			{
				_control.hintControl.hideHint();
			}
			ColorTool.saturation(this);
			this.filters = null;
		}
		
		protected function onMouseClick(event:MouseEvent):void
		{
			if (_onClickFunction != null)
			{
				_onClickFunction(event);
			}
		}

		public function get id():int
		{
			return _id;
		}
		
		

	}
}